package controllers

import (
	"github.com/gin-gonic/gin"
	"log"
	"rongyy/src/models"
	"rongyy/src/server"
	"rongyy/src/service"
	"strconv"
)

type DoctorController struct {
	*service.DoctorService
}

func NewDoctorController() *DoctorController {
	return &DoctorController{}
}

func (rc *DoctorController) Test(ctx *gin.Context) string {
	log.Println(465456465)
	return "bosuai"
}

func (qc *DoctorController) query(c *gin.Context) server.IModel {
	limit := c.Query("limit")
	limitInt, err := strconv.Atoi(limit)
	offset := c.Query("offset")
	offsetInt, err := strconv.Atoi(offset)
	json := &models.DoctorModel{}
	err = c.ShouldBindJSON(json)
	if err != nil {

	}
	data, total, err := qc.Query(limitInt, offsetInt, json.RealName)
	return &models.DataModel{IsSuccess: true, Msg: "查询成功", Data: map[string]interface{}{
		"list":  data,
		"total": total}}
}

func (qc *DoctorController) update(c *gin.Context) server.IModel {
	json := &models.DoctorModel{}
	err := c.ShouldBindJSON(json)
	if err != nil {
		return &models.DataModel{IsSuccess: true, Msg: "错误"}
	}
	err = qc.Update(json)
	return &models.DataModel{IsSuccess: true, Msg: "修改成功"}
}

func (sc *DoctorController) add(c *gin.Context) server.IModel {
	req := &models.DoctorModel{}
	err := c.ShouldBindJSON(req)
	if err != nil {
		return &models.DataModel{IsSuccess: false, Msg: "参数错误"}
	}
	err = sc.Add(req)
	if err != nil {
		return &models.DataModel{IsSuccess: false, Msg: err.Error()}
	}
	return &models.DataModel{IsSuccess: true, Msg: "添加成功"}
}

func (qc *DoctorController) delete(c *gin.Context) server.IModel {
	json := &models.DoctorModel{}
	err := c.ShouldBindJSON(json)
	if err != nil {
		return &models.DataModel{IsSuccess: true, Msg: "错误"}
	}
	err = qc.Delete(json)
	return &models.DataModel{IsSuccess: true, Msg: "删除成功"}
}

func (qc *DoctorController) updateSn(c *gin.Context) server.IModel {
	err := qc.UpdateSn()
	if err != nil {
		return &models.DataModel{IsSuccess: false, Msg: err.Error()}
	}
	return &models.DataModel{IsSuccess: true, Msg: "成功"}
}

//

func (this *DoctorController) RouterHandler(server *server.Server) {
	server.Handle("POST", "/add", this.add)
	//server.Handle("POST", "/relation/create", this.createRelationNew)
	//server.Handle("POST", "/findTodayRecord", this.FindTodayRecord)
	//server.Handle("POST", "/relationFindTodayRecord", this.relationFindTodayRecord)
	server.Handle("POST", "/query", this.query)
	server.Handle("POST", "/update", this.update)
	server.Handle("POST", "/delete", this.delete)
	server.Handle("GET", "/update-sn", this.updateSn)
}
